/**
 * Created by gf on 2017/03/20.
 * jQuery扩展
 */
(function ($) {
  //初始化notice
  var noticeBg = $('<div class="tip-panel"></div>');
  $('body').append(noticeBg);
  //提示功能
  var toastTimer;
  //notice方法
  $.extend({
    /**
     * @param content 提示内容
     * @param type 提示类别_默认为Success 可选值_success/info/warning/danger
     * @param time 提示显示停留时间_默认为2000ms
     */
    showNotice: function (content, type, time) {
      //消息中心
      if (type == "danger") {
        //处理时间函数
        function getNowFormatDate() {
          function fix(num) {
            if (num >= 0 && num <= 9)
              return "0" + num;
            return num
          }

          var date = new Date();
          var now1 = date.getFullYear() + "-" + fix(date.getMonth() + 1) + "-" + fix(date.getDate()) + " " +
            fix(date.getHours()) + ":" + fix(date.getMinutes()) + ":" + fix(date.getSeconds());
          var now2 = fix(date.getHours()) + ":" + fix(date.getMinutes()) + ":" + fix(date.getSeconds());
          return now1;
        }

        var systemMsg = {};
        systemMsg.content = content;
        systemMsg.type = type;
        systemMsg.time = getNowFormatDate();
        //$("#nav-bar").scope().systemMsgs.push(systemMsg);
        $("#nav-bar").scope().systemMsgs.splice(0, 0, systemMsg);
        $("#nav-bar").scope().hasNew = $("#nav-bar").scope().hasNew + 1;
        // console.log($("#nav-bar").scope().hasNew);

        setTimeout(function () {
          $('.msg-dropdown').click(function (e) {
            e.stopPropagation();
          });
          $('[data-toggle="tooltip"]').tooltip();
        }, 0);
      }

      var notice = $('<div class="alert alert-success tip" role="alert"></div>');
      if (type && type != 'success') notice = $('<div class="alert alert-' + type + ' tip" role="alert"></div>');
      noticeBg.html(notice);
      notice.append(content);

      if (toastTimer) clearTimeout(toastTimer);
      //动画时长ms
      var aniTime = 150;
      //超时关闭ms
      var outTime = time ? time : 3000;
      noticeBg.show().animate({
        opacity: "1",
        bottom: '0'
      }, aniTime);
      toastTimer = setTimeout(function () {
        noticeBg.animate({
          opacity: "0",
          bottom: '-18px'
        }, aniTime).delay(aniTime).hide(0);
      }, outTime);
    },
    hideNotice: function () {
      noticeBg.hide();
    }
  });
}(jQuery));